package com.higgins.client.agentapp.components;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.SelectElement;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.Cell;
import com.higgins.client.GreetingService;
import com.higgins.client.GreetingServiceAsync;
import com.higgins.client.agentapp.datamodel.AgencyDTO;
import com.higgins.client.agentapp.datamodel.AgentDTO;
import com.higgins.client.agentapp.datamodel.JobSiteDTO;
import com.higgins.client.agentapp.service.AgentAppService;
import com.higgins.client.agentapp.service.AgentAppServiceAsync;
import com.higgins.client.dataModel.CoveringLetterDTO;
import com.higgins.client.dataModel.RecipientCountDTO;
import com.higgins.client.dataModel.StatusDTO;
import com.higgins.client.dataModel.StrategyDTO;
import com.higgins.client.dataModel.StrategyRecipientCountDTO;
import com.higgins.client.dataModel.StrategyRecipientDTO;
import com.higgins.client.generic.components.GenericColumn;
import com.higgins.client.generic.components.GenericTable;

public class AgentTable extends GenericTable	{
	private static final Logger log = Logger.getLogger(AgentTable.class.getName());
	private final AgentAppServiceAsync agentAppService = GWT
			.create(AgentAppService.class);
	private AgencyDTO agencyDTO;
	
	public AgentTable(AgentColumns columns)	{
		super(columns);
	}
	
	public void update(AgencyDTO agencyDTO)	{
		this.agencyDTO = agencyDTO;
		retrieveDTOs(agencyDTO);
	}
	
	protected void addObject(int row, int column, Object object)	{
		ensureRowAvailable(row);
		GenericColumn genericColumn = genericColumns.getColumn(column);
//		if (genericColumn.getType().equals(StatusDTO.class))	{
//			addStatusDTO(row, column, object, genericColumn);
//		}
	}
	
	private List<List<Object>> convertDTOtoRowArray(List<AgentDTO> agentDTOs)	{
		List<List<Object>> rows = new ArrayList<List<Object>>();
		for (AgentDTO agentDTO:agentDTOs)	{
			List<Object> row = new ArrayList<Object>();
			row.add(agentDTO.getId());
			row.add(agentDTO.getName());
			row.add(agentDTO.getPhone());
			row.add(agentDTO.getEmail());
			row.add(agentDTO.getComment());
			row.add(agentDTO.getRank()!=null?agentDTO.getRank():"1");
			rows.add(row);
		}
		return rows;
	}
	
	public void updateItemCell(int row)	{
		AgentDTO agentDTO = new AgentDTO();
		agentDTO.setId(getId(row));
		agentDTO.setName(getText(row, 1));
		agentDTO.setPhone(getText(row, 2));
		agentDTO.setEmail(getText(row, 3));
		agentDTO.setComment(getText(row, 4));
		agentDTO.setRank(getInteger(row, 5));
		agentDTO.setAgencyDTO(agencyDTO);
		updateRow(agentDTO);
	}
	
	protected void retrieveDTOs(AgencyDTO agencyDTO)	{
		try {
			agentAppService.retrieveAgents(agencyDTO.getId(), new AsyncCallback<List<AgentDTO>>()	{
					@Override
					public void onFailure(Throwable caught) {
						log.info("FixedOutgoingDTO: " + caught.toString());
					}

					@Override
					public void onSuccess(List<AgentDTO> result) {
						loadData(convertDTOtoRowArray(result));
					}
				});
		} catch (IllegalArgumentException e) {
			log.info("FixedOutgoingDTO: " + e.toString());
		} 
	}	
	
	private void updateRow(AgentDTO agentDTO)	{
		try {
			agentAppService.updateAgent(agentDTO, new AsyncCallback<String>()	{
					@Override
					public void onFailure(Throwable caught) {
						log.info("FixedOutgoingDTO: " + caught.toString());
					}

					@Override
					public void onSuccess(String result) {
						setRowIdAfterUpdate(result);
					}
				});
		} catch (IllegalArgumentException e) {
			log.info("FixedOutgoingDTO: " + e.toString());
		}
	}
}